package com.dylan.历史.领扣;

/**
 * @Auther: ankang
 * @Date: 2019/3/26 10:48
 * @Description:
 */
public class $0071简化路径 {

    public static void main(String[] args) {
        String simplifyPath = new $0071简化路径.Solution().simplifyPath("/a//b////c/d//././/..");
        System.out.println(simplifyPath);
    }

    static class Solution {
        public String simplifyPath(String path) {
            String[] result = new String[path.length()];
            int resultIndex = 0;
            StringBuilder temp = new StringBuilder(path.length());
            for (int i = 0, len = path.length(); i < len; i++) {
                char c = path.charAt(i);
                if ('/' == c) {
                    String tempStr = temp.toString();
                    if ("..".equals(tempStr)) {
                        if (resultIndex != 0) {
                            resultIndex--;
                        }
                    } else if (!".".equals(tempStr) && tempStr.length() != 0) {
                        result[resultIndex] = tempStr;
                        resultIndex++;
                    }
                    temp.delete(0, temp.length());
                } else {
                    temp.append(c);
                }
            }
            String tempStr = temp.toString();
            if ("..".equals(tempStr)) {
                if (resultIndex != 0) {
                    resultIndex--;
                }
            } else if (!".".equals(tempStr) && tempStr.length() != 0) {
                result[resultIndex] = tempStr;
                resultIndex++;
            }
            String[] result2 = new String[resultIndex];
            System.arraycopy(result, 0, result2, 0, resultIndex);
            return result.length == 0 ? "/" : "/" + String.join("/", result2);
        }
    }
}
